package topday1_9.customer.bean.view;

import topday1_9.customer.bean.Customer;
import topday1_9.customer.bean.service.CustomerList;

import java.util.Scanner;

/**
 * @Auther:小陈的Java
 * 客户数据显示层编写
 */
public class CustomerView {
    CustomerList customerList = new CustomerList(10);
    Scanner scanner = new Scanner(System.in);
    private void mainMenu() {
        while(true) {
            System.out.println("\r\n--------------------客户管理系统--------------------");
            System.out.println("                    1.添加客户");
            System.out.println("                    2.修改客户");
            System.out.println("                    3.删除客户");
            System.out.println("                    4.客户列表");
            System.out.println("                    5.退出");
            System.out.println("请选择【1-5】");
            int number = scanner.nextInt();
            if(!(number==1||number==2||number==3||number==4||number==5)){
                System.out.println("输入有误，请重新输入");
                continue;
            }
            a:switch(number) {
                case 1:
                    addCustomer();
                    break;
                case 2:
                    modifyCustomer();
                    break;
                case 3:
                    deleteCustomer();
                    break;
                case 4:
                    viewCustomerList();
                    break;
                case 5:
                    while(true) {
                        System.out.println("确认是否退出y/n");
                        String str = scanner.next();
                        if(str.equalsIgnoreCase("y")) {
                            return;
                        }else if(str.equals("n")) {
                            break a;
                        }
                    }


            }
        }
    }
    private void viewCustomerList() {
        System.out.println("--------------------客户列表--------------------");
        Customer[] customersAll = customerList.getCustomerList();
        if(customersAll.length==0) {
            System.out.println("没有客户记录");
        } else {
            System.out.println("编 号\t姓 名\t性 别\t年 龄\t手机号\t\t邮 箱\t");
            for(int i=0;i<customersAll.length;i++) {
                System.out.println((i+1)+"\t"+customersAll[i].getName()+"\t"+customersAll[i].getSex()+"\t"+customersAll[i].getAge()+"\t"+customersAll[i].getPhone()+"\t"+customersAll[i].getEmial());
            }
        }
    }
    private void addCustomer() {
        System.out.println("--------------------添加客户--------------------");
        System.out.println("姓名");
        String name = scanner.next();
        System.out.println("性别");
        String sex = scanner.next();
        System.out.println("年龄");
        int age = scanner.nextInt();
        System.out.println("手机号");
        String phone = scanner.next();
        System.out.println("邮箱");
        String email = scanner.next();
        Customer customer = new Customer(name, sex, age, phone, email);
        boolean tag = customerList.add(customer);
        if(tag) {
            System.out.println("添加成功");
        } else {
            System.out.println("记录已满，添加失败");
        }
    }
    private void modifyCustomer() {
        System.out.println("--------------------修改客户--------------------");
        int index = 0;
        Customer customer;
        while(true) {
            System.out.println("请选择要修改的客户编号【-1退出】");
            index = scanner.nextInt();
            if(index==-1) {
                return;
            } else {
                customer = customerList.getCustomer(index-1);
                if(customer==null) {
                    System.out.println("无法找到要修改的客户");
                } else {
                    break;
                }
            }
        }
        System.out.println("姓名("+customer.getName()+"):");
        String name = scanner.next();
        System.out.println("性别("+customer.getSex()+"):");
        String sex = scanner.next();
        System.out.println("年龄("+customer.getAge()+"):");
        int age = scanner.nextInt();
        System.out.println("手机号("+customer.getPhone()+"):");
        String phone = scanner.next();
        System.out.println("邮箱("+customer.getEmial()+"):");
        String email = scanner.next();
        customer = new Customer(name, sex, age, phone, email);
        boolean tag = customerList.modify(index-1, customer);
        if(tag) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
    }
    private void deleteCustomer() {
        System.out.println("--------------------删除客户--------------------");
        int index = 0;
        Customer customer;
        while(true) {
            System.out.println("请选择要修改的客户编号【-1退出】");
            index = scanner.nextInt();
            if(index==-1) {
                return;
            } else {
                customer = customerList.getCustomer(index-1);
                if(customer==null) {
                    System.out.println("无法找到要删除的客户");
                } else {
                    break;
                }
            }
        }
        while(true) {
            System.out.println("确认删除y/n");
            String str = scanner.next();
            if(str.equalsIgnoreCase("y")) {
                break;
            }else if(str.equalsIgnoreCase("n")) {
                return;
            }

        }
        boolean tag = customerList.delete(index-1);
        if(tag) {
            System.out.println("删除成功");

        }else {
            System.out.println("删除失败");
        }
    }
    public static void main(String[] args) {
        CustomerView customerView = new CustomerView();
        customerView.mainMenu();
    }
}
